package cn.com.homeyoda.aimate.aihealth.ui.health_qa.result;

import static cn.com.homeyoda.aimate.aihealth.utils.AppConstants.ID;
import static cn.com.homeyoda.aimate.aihealth.utils.AppConstants.QUIZ_RECORD_ID;
import static cn.com.homeyoda.aimate.aihealth.utils.AppConstants.TYPE;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import androidx.lifecycle.ViewModelProviders;

import com.wzq.mvvmsmart.utils.ToastUtils;

import cn.com.homeyoda.aimate.aihealth.BR;
import cn.com.homeyoda.aimate.aihealth.R;
import cn.com.homeyoda.aimate.aihealth.app.AppViewModelFactory;
import cn.com.homeyoda.aimate.aihealth.base.BaseActivity;
import cn.com.homeyoda.aimate.aihealth.databinding.ActivityHealthQaResultBinding;
import cn.com.homeyoda.aimate.aihealth.ui.health_qa.HealthQaActivity;
import cn.com.homeyoda.aimate.aihealth.ui.health_qa.HealthQaViewModel;

public class HealthQaResultActivity extends BaseActivity<ActivityHealthQaResultBinding, HealthQaViewModel> implements View.OnClickListener {

    private String id;
    private String type;

    public static void start(Context context, String id, String type, String quizRecordId) {
        Intent starter = new Intent(context, HealthQaResultActivity.class);
        starter.putExtra(ID, id);
        starter.putExtra(TYPE, type);
        starter.putExtra(QUIZ_RECORD_ID, quizRecordId);
        context.startActivity(starter);
    }

    @Override
    public HealthQaViewModel initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(HealthQaViewModel.class);
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_health_qa_result;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        super.initData();
        id = getIntent().getStringExtra(ID);
        type = getIntent().getStringExtra(TYPE);
        viewModel.getAnswerAnalysis(getIntent().getStringExtra(QUIZ_RECORD_ID));
        initView();
    }

    private void initView() {
        binding.titleBar.ivBack.setOnClickListener(this);
        binding.titleBar.tvTitle.setText(getString(R.string.health_qa));

        binding.btnDownloadReport.setOnClickListener(this);
        binding.btnOneMoreTime.setOnClickListener(this);

//        binding.wbContent.setBackgroundColor(Color.TRANSPARENT);
//        binding.wbContent.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
//        binding.wbContent.loadDataWithBaseURL(null, viewModel.htmlContent, "text/html", "UTF-8", null);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.answerAnalysisData.observe(this, data -> {
            if (data != null) {
                binding.tvTitle.setText(data.getQuizName());
                binding.tvAnswerDate.setText("答题时间：" + data.getAddTimeStr());
                binding.tvAnswerUseTime.setText("答题用时：" + data.getDurationTime());
                binding.explanation.setText(data.getAnswerExplanation());
            }
        });
    }

    @Override
    public void onClick(View v) {
        int redId = v.getId();
        if (redId == R.id.iv_back) {
            finish();
        } else if (redId == R.id.btn_download_report) {
            ToastUtils.showShort("下载报告");
        } else if (redId == R.id.btn_one_more_time) {
            HealthQaActivity.start(this, id, type);
            finish();
        }
    }

}